package com.yonyou.cyxdms.sysAuth.service.controller.dealerchannel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.CompanyDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.CompanyPO;
import com.yonyou.cyxdms.sysAuth.service.service.dealerchannel.DealerChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by adu on 2019/10/12.
 */
@RestController
@RequestMapping("/CompanyInfo")
@Api(value = "经销商公司维护", tags = "经销商公司维护-Api")
public class CompanySaleInfoController {
    private final Logger logger = LoggerFactory.getLogger(CompanySaleInfoController.class);
    @Autowired
    private DealerChannelService dealerChannelService;

    /**
     * 查询-所属经销商公司
     */
    @GetMapping(value = "/getCompany")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "查询-经销商公司维护列表(分页)")
    public IPage<CompanyPO> getCompanyInfo(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "companyCode", required = false) String companyCode,
                                           @RequestParam(value = "companyName", required = false) String companyName,
                                           @RequestParam(value = "provinceId", required = false) String provinceId,
                                           @RequestParam(value = "cityId", required = false) Integer cityId) {
        Page<Map<String, Object>> page = new Page(currentPage, pageSize);
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        Map<String, Object> queryCondition = new HashMap<>();
        queryCondition.put("appId", loginInfoDto.getAppId());
        //  queryCondition.put("dataType", ManageDictCodeConstants.DATA_TYPE_BY_SUPER);
        queryCondition.put("companyType", ManageDictCodeConstants.ORG_TYPE_DEALER_COMPANY);
        queryCondition.put("companyCode", companyCode);
        queryCondition.put("companyName", companyName);
        queryCondition.put("provinceId", provinceId);
        queryCondition.put("cityId", cityId);
        return dealerChannelService.getDistributorList(page, queryCondition);
    }


    /**
     * 新增-经销商公司
     * @author adu
     * @since 2019/10/12
     */
    @PostMapping("/addCompany")
    @ResponseStatus(HttpStatus.OK)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "新增（编辑）-经销商公司")
    public Integer addCompany(@RequestBody CompanyDTO companyDTO) {
        return dealerChannelService.addCompany(companyDTO);
    }


    /**
     * 查看-所属经销商公司
     */
    @GetMapping(value = "/getCompany/{companyId}")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @ApiOperation(value = "查看-经销商公司")
    public CompanyDTO getCompanyId(@PathVariable("companyId")Integer companyId) {
        return dealerChannelService.getCompanyId(companyId);
    }


    /**
     * 查看-查询车厂code
     * @author maxingjian
     * @since 2019/12/21
     */
    @GetMapping(value = "/getGroupCodeByAppId")
    @ApiOperation(value = "查询车厂code")
    public String getGroupCodeByAppId(@RequestParam("appId") String appId) {
        return dealerChannelService.getGroupCodeByAppId(appId);
    }


}
